@extends('admin.main')
@section('content')
    <div class="admin-content">
        <div class="am-cf am-padding am-padding-bottom-0">
            <div class="am-fl am-cf">
                <strong class="am-text-primary am-text-lg">后台设置</strong> /
                <small>企业资料</small>
            </div>
        </div>
        <hr>
        <div class="am-g">
            @include('admin.layouts.adminInfo')
            <div class="am-u-sm-12 am-u-md-8 am-u-md-pull-4">
                <form action="{{$prefix_url}}{{isset($data)?'/'.$data->id:''}}"
                      class="am-form" method="POST" enctype="multipart/form-data">
                    {{csrf_field()}}
                    <input type='hidden' name='_method' value='POST'/>
                    <input type='hidden' name='uid' value='{{$uid}}'/>
                    <div class='am-form-group'>
                        <h2>法人信息</h2>
                    </div>
                    <div class='am-form-group'>
                        <label>法人姓名：</label>
                        <input type="text" name="legal_name" placeholder="法人姓名" minlength='1' maxlength='20' required value="{{isset($data)?$data->legal_name:''}}">
                    </div>
                    <div class='am-form-group'>
                        <label>法人性别：</label>
                        <select name="legal_sex" required>
                            <option value="1" {{(isset($data)&&$data->legal_sex==1)?'selected':''}}>男</option>
                            <option value="2" {{(isset($data)&&$data->legal_sex==2)?'selected':''}}>女</option>
                        </select>
                    </div>
                    <div class='am-form-group'>
                        <label>法人手机号码：</label>
                        <input type="text" name="legal_mobile" placeholder="法人手机号码" pattern="^1[34578]\d{9}$" required value="{{isset($data)?$data->legal_mobile:''}}">
                    </div>
                    <div class='am-form-group'>
                        <label>法人身份证号码：</label>
                        <input type="text" name="legal_idcard" placeholder="法人身份证号码" minlength='17' maxlength='20' required value="{{isset($data)?$data->legal_idcard:''}}">
                    </div>
                    <div class='am-form-group'>
                        <label>法人身份证正面：</label>
                        @if(isset($data) && $data->idcardImg0
                            && file_exists(ltrim($data->idcardImg0,'/')))
                            (上传图片将覆盖当前图片)
                        @endif
                        <input type="file" name="idcardImg0"
                               style="padding:5px;width:100%;border:1px solid #cccccc;background:#f0f0f0;">
                        @if(isset($data) && $data->idcardImg0
                            && file_exists(ltrim($data->idcardImg0,'/')))
                            <p>当前照片：</p>
                            <img src="{{$data->idcardImg0}}" width="500">
                        @endif
                    </div>
                    <div class='am-form-group'>
                        <label>法人身份证反面：</label>
                        @if(isset($data) && $data->idcardImg1
                            && file_exists(ltrim($data->idcardImg1,'/')))
                            (上传图片将覆盖当前图片)
                        @endif
                        <input type="file" name="idcardImg1"
                               style="padding:5px;width:100%;border:1px solid #cccccc;background:#f0f0f0;">
                        @if(isset($data) && $data->idcardImg1
                            && file_exists(ltrim($data->idcardImg1,'/')))
                            <p>当前照片：</p>
                            <img src="{{$data->idcardImg1}}" width="500">
                        @endif
                    </div>
                    <div class='am-form-group'>
                        <label>法人身份证手持正面：</label>
                        @if(isset($data) && $data->idcardImg2
                            && file_exists(ltrim($data->idcardImg2,'/')))
                            (上传图片将覆盖当前图片)
                        @endif
                        <input type="file" name="idcardImg2"
                               style="padding:5px;width:100%;border:1px solid #cccccc;background:#f0f0f0;">
                        @if(isset($data) && $data->idcardImg2
                            && file_exists(ltrim($data->idcardImg2,'/')))
                            <p>当前照片：</p>
                            <img src="{{$data->idcardImg2}}" width="500">
                        @endif
                    </div>
                    <br><hr>
                    <div class='am-form-group'>
                        <h2>公司信息</h2>
                    </div>
                    <br>
                    <div class='am-form-group'>
                        <label>公司名称：</label>
                        <input type="text" name="com_name" placeholder="公司名称" minlength='2' maxlength='20' required value="{{isset($data)?$data->com_name:''}}">
                    </div>
                    <div class='am-form-group'>
                        <label>公司座机：</label>
                        <input type="text" name="com_tel" placeholder="公司座机" value="{{isset($data)?$data->com_tel:''}}">
                    </div>
                    <div class='am-form-group'>
                        <label>公司传真：</label>
                        <input type="text" name="com_fax" placeholder="公司传真" value="{{isset($data)?$data->com_fax:''}}">
                    </div>
                    <div class='am-form-group'>
                        <label>公司地址：</label>
                        <input type="text" name="com_address" placeholder="公司地址" value="{{isset($data)?$data->com_address:''}}">
                    </div>
                    <div class='am-form-group'>
                        <label>公司所在城市：</label>
                        <select id="provice">
                            <option value="0">省</option>
                        </select><br>
                        <select id="city">
                            <option value="0">市</option>
                        </select><br>
                        <select name="com_city" id="area">
                            <option value="0">区</option>
                        </select>
                        <input type="hidden" id="proviceId" value="{{isset($data)?$data->proviceId:0}}">
                        <input type="hidden" id="cityId" value="{{isset($data)?$data->cityId:0}}">
                        <input type="hidden" id="areaId" value="{{isset($data)?$data->areaId:0}}">
                    </div>
                    <div class='am-form-group'>
                        <label>企业类型：</label>
                        <select name="cate" required>
                            <option value="1" {{(isset($data)&&$data->cate==1)?'selected':''}}>仅工厂</option>
                            <option value="2" {{(isset($data)&&$data->cate==2)?'selected':''}}>仅公司</option>
                            <option value="3" {{(isset($data)&&$data->cate==3)?'selected':''}}>公司含工厂</option>
                        </select>
                        <input type="hidden" id="cateId" value="{{isset($data)?$data->cate:1}}">
                    </div>
                    <div class='am-form-group'>
                        <label>客户满意度：</label>
                        <input type="text" name="cus_like" placeholder="客户满意度，百分比" value="{{isset($data)?$data->cus_like:''}}">
                    </div>
                    <div class='am-form-group'>
                        <label>品质合格率：</label>
                        <input type="text" name="quality" placeholder="品质合格率，百分比" value="{{isset($data)?$data->quality:''}}">
                    </div>
                    <div class='am-form-group'>
                        <label>合同履行率：</label>
                        <input type="text" name="pact" placeholder="合同履行率，百分比" value="{{isset($data)?$data->pact:''}}">
                    </div>
                    <div class='am-form-group'>
                        <label>年龄段：</label>
                        <input type="text" name="ageStart" placeholder="几岁" value="{{isset($data)?$data->ageStart:''}}">
                        -
                        <input type="text" name="ageEnd" placeholder="几岁" value="{{isset($data)?$data->ageEnd:''}}">
                    </div>
                    <div class='am-form-group'>
                        <label>业务类型：</label>
                        <span id="outType"></span>
                        <input type="hidden" id="outTypeIdStr" value="{{isset($data)?$data->out_type:''}}">
                    </div>
                    <span id="param">
                    </span>
                    <button type='button' class='am-btn am-btn-primary'
                            onclick='history.go(-1);'>返回</button>
                    <button type='submit' class='am-btn am-btn-primary'>
                        保存更新</button>
                </form>
                <br><br>
            </div>
        </div>
    </div>

    <script>
//        //日期组件
//        $(function() {
//            $('.doc-datepicker').datepicker().
//            on('changeDate.datepicker.amui', function(event) {
//                console.log(event.date);
//            });
//        });
        //初始化
        (function(){
            var cate = $("select[name='cate']").val();
            var proviceId = $('#proviceId').val();
            var cityId = $('#cityId').val();
            proviceId = proviceId==0?110000:proviceId;
            cityId = cityId==0?110100:cityId;
            getArea(100000,1); //获取省
            getArea(proviceId,2); //获取市
            getArea(cityId,3); //获取区
            getOutTypes(cate);
            getParam(cate);
        })();
        /**
         * 动态更新
         * */
        $("select[name='cate']").on('change',function(){
            var cate = $("select[name='cate']").val();
            $('#cateId')[0].value = cate;
            var cateId = $('#cateId').val();
            getOutTypes(cateId); //业务类型
            getParam(cateId); //动态参数
        });
        /**
         * 更新省市区
         * */
        $("#provice").on('change',function(){
            var provice = $("#provice").val();
            if (!provice) {
                alert('没有选择省！'); return
            }
            getArea(provice,2);
        });
        $("#city").on('change',function(){
            var city = $("#city").val();
            if (!city) {
                alert('没有选择市！'); return
            }
            getArea(city,3);
        });
        function getArea(pid,level){
            var proviceId = $('#proviceId').val();
            var cityId = $('#cityId').val();
            var areaId = $('#areaId').val();
            var formData = {
                pid:pid,
                level:level
            };
            $.ajaxSetup({
                headers: { 'X-CSRF-TOKEN':$("input[name='_token']").val() }
            });
            $.ajax({
                type:'post',
                dataType:'json',
                url:'/mcadmin/basic/member/company/citysbypid',
                data:formData,
                success:function(res){
                    //console.log(res); return
                    if (res.code!=0) {
                        console.log(res.msg);
                        return
                    }
                    console.log(res.data);
                    var resData = res.data;
                    var i, h = '';
                    for (i=0;i<resData.length;i++) {
                        h += '<option value="'+resData[i].id+'" ';
                        if (level==1 && proviceId!=0) {
                            h += ' selected';
                        } else if (level==2 && cityId!=0) {
                            h += ' selected';
                        } else if (level==3 && areaId!=0) {
                            h += ' selected';
                        }
                        h += '>';
                        h += resData[i].name+'</option>>';
                    }
                    if (level==1) {
                        $("#provice").html(h);
//                        var noData = "<option value='0'>选择</option>";
//                        $("#city").html(noData);
//                        $("#area").html(noData);
                    }
                    else if (level==2) {
                        $("#city").html(h);
//                        var noData = "<option value='0'>选择</option>";
//                        $("#area").html(noData);
                    }
                    else if (level==3) {
                        $("#area").html(h);
                    }
                },
                error:function(){
                    console.log('操作失败！');
                }
            });
        }
        /**
         * 业务类型
         * */
        function getOutTypes(cate){
            var outTypeIdStr = $('#outTypeIdStr').val();
            var outTypeIds  = [];
            if (outTypeIdStr) {
                outTypeIds = outTypeIdStr.split('||');
            }
            var formData = {
                cate:cate
            };
            $.ajaxSetup({
                headers: { 'X-CSRF-TOKEN':$("input[name='_token']").val() }
            });
            $.ajax({
                type:'post',
                dataType:'json',
                url:'/mcadmin/basic/member/company/catesbypid',
                data:formData,
                success:function(res){
                    //console.log(res); return
                    if (res.code!=0) {
                        console.log(res.msg);
                        return
                    }
//                    console.log(res.data);
                    var resData = res.data;
                    var i, h = '';
                    if (resData.length > 0) {
                        h += '<br>';
                        for (i=0;i<resData.length-1;i++) {
                            h += '<label>';
                            h += '<input type="checkbox" name="out_types[]"';
                            h += ' value="'+resData[i].id+'"';
                            if (outTypeIds.includes(resData[i].id)==true) {
                                h += ' checked';
                            }
                            h += '/> ';
                            h += resData[i].name;
                            h += '&nbsp; &nbsp; ';
                            h += '</label>';
                        }
                    }
                    $("#outType").html(h);
                },
                error:function(){
                    console.log('操作失败！');
                }
            });
        }
        /**
         * 动态参数
         * */
        function getParam(cate){
            var param = '';
            //1仅工厂，2仅公司，3公司含工厂
            if (cate==1) {
                param = getFacParam();
            } else if (cate==2){
                param = getComParam();
            } else {
                param = getCom2Param();
            }
//            console.log(param[0].key);
            var h = '';
            for (var i=0;i<param.length;i++) {
                h += '<div class="am-form-group">';
                h += '<label>'+param[i].name+'：</label>';
                h += '<input type="text" name="'+param[i].key+'" placeholder="'+param[i].name+'">';
                h += '</div>';
            }
            $('#param').html(h);
        }
        //仅工厂
        function getFacParam(){
            return [
                { key:'output', name:'年产量' },
                { key:'truck', name:'送货车辆' },
                { key:'piece', name:'整件' },
                { key:'bale', name:'捆包流' },
                { key:'single', name:'单件流' },
                { key:'cut', name:'裁剪' },
                { key:'back', name:'后道' },
                { key:'total', name:'工厂总人数' }
            ];
        }
        //仅公司
        function getComParam(){
            return [
                { key:'scale', name:'公司规模' },
                { key:'totalSale', name:'公司销售额' },
                { key:'yearSale', name:'年销售额' },
                { key:'truck', name:'送货车辆' },
                { key:'brand', name:'品牌' }
            ];
        }
        //公司含工厂
        function getCom2Param(){
            return [
                { key:'output', name:'年产量' },
                { key:'truck', name:'送货车辆' },
                { key:'piece', name:'整件' },
                { key:'bale', name:'捆包流' },
                { key:'single', name:'单件流' },
                { key:'cut', name:'裁剪' },
                { key:'back', name:'后道' },
                { key:'total', name:'工厂总人数' },
                { key:'scale', name:'公司规模' },
                { key:'totalSale', name:'公司销售额' },
                { key:'yearSale', name:'年销售额' },
                { key:'brand', name:'品牌' }
            ];
        }
    </script>
@stop